<?php

class COrderContact extends Zend_Db_Table{
	
	protected $_name;
	
	protected $_primary;

	
	public function _setup(){
		
		$this->_name=R_PRE."order_contacts";
		
		$this->_primary="contactid";
		
		parent::_setup();
		
	}

	
	public function createContact($data){
		
		$contactid=$this->insert($data);

		return $contactid;
		
	}
	
	public function editContact($contactid,$data){
		
		$db=$this->getAdapter();
		
		$where = $db->quoteInto('contactid = ?', $contactid);

		$rows_affected=$db->update($this->_name,$data,$where);
		
		if($rows_affected!=1){
			
			return false;
			
		}else{
			
			return true;
			
		}
		
		
	}
	
	public function getDetail($contactid){
		
		$db=$this->getAdapter();
		
		$select = $db->select();
		
		$select->from($this->_name,'*');
		
		
		$select->where('contactid = ? ',$contactid);
		
		return $db->fetchAll($select);
		
	}
	
	public function getContactByUid($uid){
		
		$db=$this->getAdapter();
		
		$select = $db->select();
		
		$locationTable=R_PRE.'locationfls';
		
		$memberTable=R_PRE.'members';
		
		$select->from($this->_name,'*');
		
		$select->where($this->_name.'.uid = ? ',$uid);
		
		$select->joinLeft($locationTable,$locationTable.".flid=$this->_name.location_id",'*');
		
		$select->joinLeft($memberTable,$memberTable.".uid=$this->_name.uid",$memberTable.'.username');
		
		return $db->fetchAll($select);
		
	}
        public function getContactByid($id){

		$db=$this->getAdapter();

		$select = $db->select();

		$locationTable=R_PRE.'locationfls';

		$memberTable=R_PRE.'members';

		$select->from($this->_name,'*');

		$select->where($this->_name.'.contactid = ? ',$id);

		$select->joinLeft($locationTable,$locationTable.".flid=$this->_name.location_id",'*');

		$select->joinLeft($memberTable,$memberTable.".uid=$this->_name.uid",$memberTable.'.username');

		return $db->fetchAll($select);

	}
	
	public function delContacts($contactidArray){
		
		$db=$this->getAdapter();
		
		$contactidString=join(',',$contactidArray);
		
		$result=$db->query("DELETE FROM $this->_name WHERE contactid IN ($contactidString)");
		
		return $result;
		
	}
	
	
	
	public function getAllContactCounts($uid=0,$where=''){
		
		$db=$this->getAdapter();
		
		$select = $db->select();

		$select->from($this->_name,'COUNT(*)');
		
		if($where!=''){
			
			$select->where($where);
	
		}

		
		if($uid!=0&&$uid!=''){
			
			$select->where(' uid = ? ',$uid);
			
		}
		
		return $db->fetchOne($select);
		
	}
	
	public function getAllContacts($from,$counts,$uid=0,$where=''){
		
		$db=$this->getAdapter();
		
		$itemTable=$this->_name;
		
		$select = $db->select();
		
		$select->from($itemTable,'*');
		
		if($uid!=0&&$uid!=''){
			
			$select->where($itemTable.'.uid = ? ',$uid);
			
		}
		

		
		if($where!=''){
			
			$select->where($where);
			
		}
		
		$select->order("$itemTable.contactid DESC");
		
		if($counts!=0){
		
			$select->limit($counts, $from);
		
		}
		
		return $db->fetchAll($select);
		
	}
	
	public function getContactLoaction($where){
		
		$db=$this->getAdapter();
		
		$select = $db->select();
		
		$locationTable=R_PRE.'locationfls';
		
		$select->from($this->_name,"$locationTable.*");
		
		$select->join($locationTable,"$this->_name.location_id=$locationTable.flid",'*');
		
		if($where!=''){
			
			$select->where($where);
			
		}
		
		return $db->fetchAll($select);
		
	}
	
	public function getCountry($location_id){
		
		while(true){
			
			$db=$this->getAdapter();
			
			$select = $db->select();
			
			$locationTable=R_PRE.'locationfls';
			
			$select->from($locationTable,"*");
			
			$select->where("flid='$location_id'");
			
			$thisArray=$db->fetchAll($select);
			
			if($thisArray[0]['pid']==0){
				
				return $thisArray;
				
			}else{
				
				$location_id=$thisArray[0]['flid'];
				
			}
			
		}
		
		return null;
		
	}
	
	public function getUserContact($id, $uid)
	{
		$db = $this->getAdapter();
		$select = $db->select();
		$select->from($this->_name,'*');
		$select->where('contactid = ? ', $id)
			   ->where('uid = ?', $uid);
		
		return $db->fetchAll($select);
	}

	
}